/**
 * 
 */
package com.sig.sigandroidDisconnected;

import com.sig.load.LoadAllElements;
import com.sig.sigandroidDisconnected.DisconnectedActivity.DetectLocation;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

/**
 * @author zou
 *
 */
public class CarteGLSufaceView extends GLSurfaceView {
	
	private MyRender mapRenderer;
	// Offsets for touch events	 
    private float xPrevious=0.0f;
    private float yPrevious=0.0f;
    
    
    /**
     * Scale detector.
     */
    private ScaleGestureDetector scaleDetector;

    /**
     * Pointer id for dragging.
     */
    private int activePointerId = -1;

    /**
     * Last touch x-coordinate.
     */
    private float lastTouchX;

    /**
     * Last touch y-coordinate.
     */
    private float lastTouchY;

    /**
     * Current touch x-coordinate.
     */
    private float posX;

    /**
     * Current touch y-coordinate.
     */
    private float posY;

	public CarteGLSufaceView(Context context,LoadAllElements graphics,DetectLocation location) {
		super(context);
		mapRenderer=new MyRender(graphics,location);
		scaleDetector = new ScaleGestureDetector(context,
                mapRenderer.getScaleListener());
		setRenderer(mapRenderer);
	}
	
	public CarteGLSufaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// Scale.
        scaleDetector.onTouchEvent(event);

        // Drag.
        final int action = MotionEventCompat.getActionMasked(event);
        switch (action) {
        case MotionEvent.ACTION_DOWN: {
                final int pointerIndex = MotionEventCompat.getActionIndex(event);
                final float x = MotionEventCompat.getX(event, pointerIndex);
                final float y = MotionEventCompat.getY(event, pointerIndex);

                // Remember where we started (for dragging)
                lastTouchX = x;
                lastTouchY = y;
                // Save the ID of this pointer (for dragging)
                activePointerId = MotionEventCompat.getPointerId(event, 0);
                break;
        }

        case MotionEvent.ACTION_MOVE: {
                // Find the index of the active pointer and fetch its position
                final int pointerIndex = MotionEventCompat.findPointerIndex(event,
                                activePointerId);

                final float x = MotionEventCompat.getX(event, pointerIndex);
                final float y = MotionEventCompat.getY(event, pointerIndex);

                // Calculate the distance moved
                final float dx = x - lastTouchX;
                final float dy = y - lastTouchY;
                mapRenderer.onDrag(dx, dy);

                posX += dx;
                posY += dy;

                // Remember this touch position for the next move event
                lastTouchX = x;
                lastTouchY = y;

                break;
        }
        case MotionEvent.ACTION_UP: {
                activePointerId = -1;
                break;
        }
        case MotionEvent.ACTION_CANCEL: {
                activePointerId = -1;
                break;
        }
        case MotionEvent.ACTION_POINTER_UP: {
                final int pointerIndex = MotionEventCompat.getActionIndex(event);
                final int pointerId = MotionEventCompat.getPointerId(event,
                                pointerIndex);
                if (pointerId == activePointerId) {
                        // This was our active pointer going up. Choose a new
                        // active pointer and adjust accordingly.
                        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                        lastTouchX = MotionEventCompat.getX(event, newPointerIndex);
                        lastTouchY = MotionEventCompat.getY(event, newPointerIndex);
                        activePointerId = MotionEventCompat.getPointerId(event,
                                        newPointerIndex);
                }
                break;
        }
        }
        return true;
		
	}
	
	// Hides superclass method.
		public void setRenderer(MyRender renderer) 
		{
			mapRenderer = renderer;
			super.setRenderer(renderer);
		}
	

}
